package com.jeeplus.modules.guide.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.PageBean;
import com.jeeplus.modules.guide.entity.*;
import com.jeeplus.modules.guide.mapper.MobileItemMapper;
import com.jeeplus.modules.guide.utils.MobileBean;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MobileItemService {
    private static final Logger log = LoggerFactory.getLogger(MobileItemService.class);
    private final MobileItemMapper mobileItemmapper;

    public PageBean<MobileMatterLabel> getMatterLabelList(Map<String,Object> map){
        PageBean<MobileMatterLabel> pageBean = new PageBean<>();
        PageHelper.startPage(Integer.parseInt(map.get("curPage")+""), Integer.parseInt(map.get("pageSize")+""));
        PageHelper.orderBy(" count desc,matterName asc");
        List<MobileMatterLabel> list = mobileItemmapper.getMatterLabelList(map);
        PageInfo<MobileMatterLabel> pageInfo = new PageInfo<>(list);
        pageBean.setCrurentNum(Integer.parseInt(map.get("curPage")+""));
        pageBean.setPageSize(Integer.parseInt(map.get("pageSize")+""));
        pageBean.setTotalNum((int) pageInfo.getTotal());
        pageBean.setTotalPageSize(pageInfo.getPages());
        pageBean.setDatas(pageInfo.getList());
        return pageBean;
    }

    @Transactional(readOnly = false)
    public void deleteMatterLabel(String id){
        mobileItemmapper.deleteMatterLabel(id);
    }

    @Transactional(readOnly = false)
    public void inserItem(MobileMatterLabel mobileMatterLabel){
        mobileMatterLabel.setId(UUID.randomUUID().toString());
        mobileItemmapper.inserItem(mobileMatterLabel);
    }

    @Transactional(readOnly = false)
    public void updateItem(MobileMatterLabel mobileMatterLabel){
        mobileItemmapper.updateItem(mobileMatterLabel);
    }

    @Transactional(readOnly = false)
    public void inserMatterItem(String ids,String label,String id){
        String[] str = ids.split(",");
        List<MobileMatterLabel> list = new ArrayList<>();
        for(int i=0;i<str.length;i++){
            MobileMatterLabel mml = new MobileMatterLabel();
            mml.setId(UUID.randomUUID().toString());
            mml.setLabel(label);
            mml.setOid(str[i]);
            list.add(mml);
            mobileItemmapper.deleteMatterLabelValue(str[i],label);
        }
        mobileItemmapper.deleteMatterLabel(id);
        mobileItemmapper.inserMatterItem(list);
        list.clear();
    }

    public MobileMatterLabel findLabel(String id) {
        return mobileItemmapper.findLabel(id);
    }

    public List<String> findMatterLabel(){
        return mobileItemmapper.findMatterLabel();
    }

    @Transactional(readOnly = false)
    public void insertMatterLabel(String oid,String label){
        int result = mobileItemmapper.updateMatterLabel(oid,label);
        if(result <= 0){
            mobileItemmapper.insertMatterLabel(UUID.randomUUID().toString(),oid,label);
        }
    }


    @Transactional(readOnly = false)
    public void  selectMatterLabelLose(String nowVersion){
        mobileItemmapper.updateMatterStreet(nowVersion);
        mobileItemmapper.updateMatterArea(nowVersion);
        List<MobileMatterLabel> list = mobileItemmapper.selectMatterLabelLose(nowVersion);
        if(null != list && list.size() >=1){
            for(MobileMatterLabel mm : list){
                mm.setId(UUID.randomUUID().toString());
            }
            mobileItemmapper.inserMatterItem(list);
        }
    }

    @Transactional(readOnly = false)
    public void intoLabelHistory(String label){
        try{
            mobileItemmapper.intoLabelHistory(label, DateUtils.getDateTime());
        }catch (Exception e){
            log.error(e.getMessage());
        }

    }
}
